﻿
function NemoPopup() {
    this.CssLoaded = false;
};
NemoPopup.prototype.Init = function () {
    this.UI = new Object();
    this.UI.PopupHolder = this.AddDomElements();
    this.UI.Frame = this.UI.PopupHolder.find('iframe');
};
NemoPopup.prototype.Load = function () {
    this.LoadStyle();
    this.Reload($('form'));
};
NemoPopup.prototype.Reload = function (onElement) {
    $(onElement).find('*[data-type=open-popup]').click(function (e) {
        //   if (window.parent != window) return;
        e.preventDefault();
        window.Nemo.NemoPopup.ShowPopup($(this).attr('href') + "&ispopup=1");
    });
    $(onElement).find('*[data-type=close-popup]').click(function (e) {
        if (window.parent != window) {
            e.preventDefault();
            window.parent.Nemo.NemoPopup.ClosePopup();
        }
    });
};

NemoPopup.prototype.LoadStyle = function () {
    var loadFunc = function () {
        if (document.createStyleSheet) {
            document.createStyleSheet('/Nemo/Styles/webnemo.modules.popup.css');
        }
        else {
            $("head").append($("<link href='/Nemo/Styles/webnemo.modules.popup.css' type='text/css' rel='stylesheet' />"));
        }
        window.Nemo.NemoPopup.CheckStyleSheet();
    };

    if ($('head').is('*'))
        loadFunc();
    else {
        window.setTimeout('window.Nemo.NemoPopup.LoadStyle();', 50);
    }
};
NemoPopup.prototype.AddDomElements = function () {
    var $popup = $('<div id="NemoPopup" style="display:none" />');
    $popup.prependTo($('body'));
    $('<iframe src="/Nemo/Loading.aspx" />').appendTo($popup);
    return $popup;
};
NemoPopup.prototype.CheckStyleSheet = function () {
    this.CssLoaded = this.UI.PopupHolder.css('position') == 'fixed';
    if (!this.CssLoaded)
        window.setTimeout('window.Nemo.NemoPopup.CheckStyleSheet();', 50);
};

NemoPopup.prototype.ShowPopup = function (url) {
    this.UI.PopupHolder.show();
    if (url)
        this.UI.Frame.attr('src', url);
};

NemoPopup.prototype.ClosePopup = function () {
    this.UI.PopupHolder.hide();
    this.UI.Frame.attr('src', '/Nemo/Loading.aspx');
};
var popup = new NemoPopup();
window.Nemo.RegisterController(popup);
window.Nemo.NemoPopup = popup;